package cc.bailongma.me.adapter;

import java.util.List;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cc.bailongma.me.R;
import cc.bailongma.me.entity.Parent;
import cc.bailongma.me.fragment.ConnectFragment;

public class ConnectLogListAdapter extends BaseAdapter{
	private Activity activity;
	private List<Parent> parents;
	public  int position=-1;
	public View view;
		public ConnectLogListAdapter(FragmentActivity activity,
			List<Parent> parents) {
		this.parents=parents;
		this.activity=activity;
	}

		@Override
		public int getCount() {
			return parents.size();
		}

		@Override
		public Object getItem(int position) {
			return parents.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder vh=null;
			if(convertView==null){
				convertView=LayoutInflater.from(activity).inflate(R.layout.list_item_connect, null);
				vh=new ViewHolder();
				vh.tv_serial_number=(TextView) convertView.findViewById(R.id.tv_serial_number);
				vh.tv_chengwei=(TextView) convertView.findViewById(R.id.tv_chengwei);
				vh.tv_phone=(TextView) convertView.findViewById(R.id.tv_phone);
				vh.iv_switch=(ImageView) convertView.findViewById(R.id.iv_switch);
				convertView.setTag(vh);
			}
			
			final Parent p=parents.get(position);
			vh=(ViewHolder) convertView.getTag();
			vh.tv_serial_number.setText(p.getId()+"");
			vh.tv_chengwei.setText(p.getChengwei());
			vh.tv_phone.setText(p.getPhone());
			vh.tv_chengwei.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					showInfomation(p);
				}
			});
			vh.tv_phone.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					showInfomation(p);
				}
			});
			vh.iv_switch.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					change(v,p,position);
				}
			});
			vh.iv_switch.setImageResource(R.drawable.b_switch_off);
			return convertView;
		}

		class ViewHolder{
			TextView tv_serial_number;
			TextView tv_chengwei;
			TextView tv_phone;
			ImageView iv_switch;
		}

		protected void change(View v,Parent parent,int position) {
			int resId=0;
			boolean isSwitch=parent.isSwitch();
			if(this.position==position){
			}else{
				if(this.view!=null){
				ImageView old_iv=(ImageView)this.view;
				old_iv.setImageResource(R.drawable.b_switch_off);
				parents.get(this.position).setSwitch(false);
				}
			}
			if(isSwitch){
				ConnectFragment.tv_call_to.setText("为选择 号码，请从上面选择");
				resId=R.drawable.b_switch_off;
				parent.setSwitch(false);
			}else{
				ConnectFragment.tv_call_to.setText(parent.getChengwei()+"("+parent.getPhone()+")");
				resId=R.drawable.b_switch_on;
				parent.setSwitch(true);
			}
			this.view=v;
			this.position=position;
			ImageView iv=(ImageView)v;
			iv.setImageResource(resId);
		}

		protected void showInfomation(Parent parent) {
		Toast.makeText(activity, "你点击了"+parent.getChengwei(), 0).show();
		}
		
}
